package com.coffeeloversupportlibrary.coffeelover.mercury.game;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;


import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Game;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/26 19:23
 * @projectName TurningCoffee
 * @className GetWaterBarActivity.java
 * @description TODO
 */
public class GetWaterBarActivity extends Activity {
    private SharedPreferences userInfo;
    private Activity activity = GetWaterBarActivity.this;

    private AppCompatButton btn_read, btn_getWater, btn_goComment;
    private RelativeLayout rl_click;
    private LinearLayout ll_back;

    private HttpUtils httpUtils = new HttpUtils();
    private JsonUtils jsonUtils = new JsonUtils();
    private User user;
    private Game game;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_alter_getwater);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initView();
        setListener();
    }

    void setListener(){
        WaterOnClickListener listener = new WaterOnClickListener();
        btn_read.setOnClickListener(listener);
        btn_getWater.setOnClickListener(listener);
        btn_goComment.setOnClickListener(listener);
    }

    void initView(){
        userInfo = getSharedPreferences("user", 0);
        user = jsonUtils.getUser(userInfo.getString("userInfo", ""));
        game = jsonUtils.getGame(userInfo.getString("tree", ""));

        btn_read = findViewById(R.id.btn_toreadgg);
        btn_getWater = findViewById(R.id.btn_togetgg);
        btn_goComment = findViewById(R.id.btn_tochatgg);
        rl_click = findViewById(R.id.rl_click);
        ll_back = findViewById(R.id.ll_back);
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {            }
        });
    }

    class WaterOnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_toreadgg:
                    getWater("read");
                    Log.e("READ", "onClick: 被点了" );
                    break;
                case R.id.btn_togetgg:
                    getWater("commit");
                    break;
                case R.id.btn_tochatgg:
                    getWater("share");
                    break;
                case R.id.ll_back:
                    break;
            }
        }
    }

    void getWater(String type){
        int treeId = game.getData().getTree().getId();
        Log.e("TreeId", "getWater: "+treeId);
        Call call = httpUtils.getCall("/tree/water/"+treeId+"/"+type);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("GetRead", "onResponse: "+res);
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        SharedPreferences.Editor editor = userInfo.edit();
                        editor.putString("tree" ,res);
                        editor.commit();
                   }
                });
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        finish();
        return true;
    }
}
